package com.dailyblue.example.project.bookstore.book.handler;

import com.dailyblue.example.project.bookstore.commons.util.JsonResult;
import com.dailyblue.example.project.bookstore.commons.util.ResultTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

// 全局异常处理类
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(ArithmeticException.class)
    public JsonResult arithmetic(Throwable e) {
        log.info("除数为零了！{}", e.getMessage());
        return ResultTool.fail(401, "除数不能为零");
    }

    @ExceptionHandler(IndexOutOfBoundsException.class)
    public JsonResult outOfIndex(Throwable e) {
        log.info("越界了,{}", e.getMessage());
        return ResultTool.fail(402, "长度越界了");
    }

    @ExceptionHandler(RuntimeException.class)
    public JsonResult runtimeException(Throwable e) {
        e.printStackTrace();
        return ResultTool.fail(403, "出现了问题");
    }
}

